package com.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.MusicList;
import com.entity.Data.PagerData;
import com.entity.Query.MusicListQuery;
import com.service.MusicListService;
import com.service.MusicListTypeService;

@RequestMapping("/musicList")
@Controller
public class MusicListController {

	@Autowired
	private MusicListService musicListService;
	@Autowired
	private MusicListTypeService musicListTypeService;
	
	@RequestMapping("/musicList_show")
	public String musicList_show(Model model,Integer curPage,MusicListQuery musicListQuery) {
		//如果没有页数，则页数默认为1
		if(curPage==null){
			curPage = 1;
		}
		
		PagerData musicListList = musicListService.queryByPage(musicListQuery, curPage, 5);
		model.addAttribute("musicListList", musicListList);
		
		List<Map<String,Object>> musicListTypeList = musicListTypeService.queryMusicListType();
		model.addAttribute("musicListTypeList", musicListTypeList);
		return "admin/musicList";
	}
	
	@RequestMapping("/musicList_add_show")
	public String musicList_add_show(Model model) {
		List<Map<String, Object>> musicListTypeList = musicListTypeService.queryMusicListType();
		model.addAttribute("musicListTypeList", musicListTypeList);
		return "admin/musicList-add";
	}
	
	@RequestMapping("/musicList_add_submit")
	public String musicList_add_submit(@RequestParam(required=false) MultipartFile[] wj,HttpServletRequest request) {
		MusicList musicList = new MusicList();
		//获取保存文件的服务器目录
		String saveFilePath = request.getServletContext().getRealPath("myfile/musicList/");
		//遍历每个上传文件进行保存
		for(MultipartFile mf : wj){
			//文件名
			String oldFileName = mf.getOriginalFilename();
			//扩展名
			String ext = FilenameUtils.getExtension(oldFileName);
			System.out.println(oldFileName+" "+ext);
			//新的文件名
			String newFileName = UUID.randomUUID()+"_"+oldFileName;
			//构建要保存的文件对象
			File f=new File(saveFilePath,newFileName);
			if(!f.exists()){
				f.mkdirs();
			}
			//将上传文件存到指定的文件对象上
			try {
				mf.transferTo(f);
				if("png".equals(ext)){
					musicList.setMusicListPhoto("myfile/musicList/"+newFileName);
				}else if ("jpg".equals(ext)) {
					musicList.setMusicListPhoto("myfile/musicList/"+newFileName);
				}
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String musicListName = request.getParameter("musicListName");
		String musicListIntro = request.getParameter("musicListIntro");
		String musicListTypeId = request.getParameter("musicListTypeId");
		musicList.setMusicListName(musicListName);
		musicList.setMusicListIntro(musicListIntro);
		musicList.setMusicListTypeId(Integer.parseInt(musicListTypeId));
		musicListService.addMusicList(musicList);
		return "admin/musicList-add";
	}
	
	@RequestMapping("/musicList_update_show")
	public String musicList_update_show(Integer musicListId,Model model) {
		Map<String, Object> musicListMap = musicListService.queryMusicListId(musicListId);
		model.addAttribute("musicListMap", musicListMap);
		List<Map<String, Object>> musicListTypeList = musicListTypeService.queryMusicListType();
		model.addAttribute("musicListTypeList", musicListTypeList);
		return "admin/musicList-edit";
	}
	
	@RequestMapping("/musicList_update_submit")
	public String musicList_update_submit(MusicList musicList) {
		musicListService.editMusicList(musicList);
		return "admin/musicList-edit";
	}
	
	@RequestMapping("/musicList_delete")
	public String musicList_delete(Integer musicListId) {
		musicListService.del(musicListId);
		return "admin/musicList";
	}
}
